﻿//This filter is tested and seems to work correctly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Sharpen filter
    /// </summary>
    public class Sharpen : BaseImageFilter
    {
        protected int m_nWeight = 11;
        CConvMatrix matrix = new CConvMatrix();

        /// <summary>
        /// Initializes a new instance of the <see cref="Sharpen"/> class.
        /// </summary>
        public Sharpen()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Sharpen"/> class.
        /// </summary>
        /// <param name="_nWeight">The _n weight.</param>
        public Sharpen(int _nWeight)
        {
            m_nWeight = _nWeight;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Sharpen"/> class.
        /// </summary>
        /// <param name="_sharpen">The _sharpen.</param>
        internal Sharpen(Sharpen _sharpen)
        {
            this.m_nWeight = _sharpen.m_nWeight;
        }

        /// <summary>
        /// Gets or sets the weight.
        /// </summary>
        /// <value>The weight.</value>
        public int Weight
        {
            get { return m_nWeight; }
            set { m_nWeight = value; }
        }

        /// <summary>
        /// Sharpens the specified bitmap
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
			matrix.SetAll(0);
			matrix.Pixel = m_nWeight;
			matrix.TopMid = matrix.MidLeft = matrix.MidRight = matrix.BottomMid = -2;
			matrix.Factor = m_nWeight - 8;

            new Convolution3x3(matrix).Execute(_bitmap);
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Sharpen(this);
        }
    }
}
